package com.gopay.bis.cuccpay.ebank.operation;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.cuccpay.ebank.CuccPayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.MD5Util;
import com.gopay.remote.bank.ICertHelper;

/**
 * 联通沃签名验签实现
 * 
 * @CuccPayCertHelper.java
 * @author WangHongFei
 */
public class CuccPayCertHelper implements ICertHelper{

	@Override
	public String sign(BaseMessage bsaeMessage) throws GopayException {
		try{
			CuccPayMessage message = (CuccPayMessage) bsaeMessage;
			String plainStr = message.getPlainMessage();
			BankLogUtils.printB2CSignPlainLog(message);
			String signStr = MD5Util.signType(plainStr, "GBK");
			BankLogUtils.printB2CSignSignatureLog(message, signStr);
			return signStr;
		}catch(Exception e){
			BankLogUtils.printB2CSignExceptionLog(bsaeMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6029);
		}		
	}

	@Override
	public Object verify(BaseMessage bsaeMessage) throws GopayException {
		try{
			CuccPayMessage message = (CuccPayMessage) bsaeMessage;
			BankLogUtils.printB2CVerifyPlainLog(message);			
			String signStr = MD5Util.signType(message.getPlainMessage(),"GBK");
			BankLogUtils.printB2CVerifySignatureLog(message, signStr);
			if (!StringUtils.equals(signStr, message.getSignMsg())) {
				BankLogUtils.printB2CVerifyResultLog(message, false);
				return false;
			}else{
				BankLogUtils.printB2CVerifyResultLog(message, true);
				return true;
			}
		}catch(Exception e){
			BankLogUtils.printB2CVerifyExceptionLog(bsaeMessage, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6031);
		}		
	}

}
